package com.itboye.system.helper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hebidu on 16/7/20.
 */
public class ByRegExpHelper {

    /**
     * 只能判别国内手机号
     * @param test
     * @return
     */
    public static boolean isMobile(String test){
        return Pattern.matches("^1[0-9]{10}",test);
    }

    /**
     * 邮箱格式验证
     * @param test
     * @return
     */
    public static boolean isEmail(String test){
        String pattern = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        return Pattern.matches(pattern,test);
    }

    public static boolean isLegalUserName(String username){
        if(username.length() > 48 || username.length() < 6){
            return false;
        }

        if(username.startsWith("m_")){
            //用户名不能m_+11位数字
            username = username.replace("m_","");
            System.out.println(username);
            return !isMobile(username);
        }

        return Pattern.matches("^[A-Za-z][A-Za-z0-9_]{5,47}$",username);
    }

}
